package leet;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fance
 * @date 18-5-10 上午6:56
 */
public class StringSum {

    /**
     * 771. 宝石与石头
     * @param J
     * @param S
     * @return
     */
    public int numJewelsInStones(String J, String S) {
        boolean[] map = new boolean[256];
        for (int i = 0; i < J.length(); i++) {
            map[J.charAt(i)] = true;
        }
        int res = 0;
        for (int i = 0; i < S.length(); i++) {
            if (map[S.charAt(i)]) {
                res++;
            }
        }
        return res;
    }


    // 最长公共前缀
    public String f(String[] strs) {
        if (strs == null || strs.length == 0) {
            return null;
        }
        if (strs.length == 1) {
            return strs[0];
        }
        String res = null;
        for (String s: strs
             ) {
            if (s.equals("") || s.equals(" ")) {
                return res;
            }
        }

        for (int i = 0; i < strs[0].length(); i++) {
            String cur = strs[0].substring(0,i + 1);
            for (int j = 1; j < strs.length; j++) {
                if (!strs[j].startsWith(cur)) {
                    return res;
                }
            }
            res = cur;
        }
        return res;
    }

    /**
     * 500. 键盘行
     * 自己写的 有问题?
     * @param words
     * @return
     */
    public String[] findWordsMine(String[] words) {
        String[] first = {"q","w","e","r","t","y","u","i","o","p"};
        String[] second = {"a","s","d","f","g","h","j","k","l"};
        String[] third = {"z","x","c","v","b","n","m"};
        List<String> res = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            if (judgeForFindWords(words[i].toLowerCase(),first) ||
                    judgeForFindWords(words[i].toLowerCase(),second) ||
                    judgeForFindWords(words[i].toLowerCase(),third)) {
                res.add(words[i]);
            }
        }
        String[] ret = new String[res.size()];
        for (int i = 0; i < res.size(); i++) {
            ret[i] = res.get(i);
        }
        return ret;
    }
    private boolean judgeForFindWords(String s,String[] arr) {
        for (int i = 0; i < s.length(); i++) {
            String cur = String.valueOf(s.charAt(i));
            boolean flag = false;
            for (int j = 0; j < arr.length; j++) {
                if (cur.equals(arr[i])) {
                    flag = true;
                }
            }
            if (!flag) {
                return false;
            }
        }
        return true;
    }

    public String[] findWords(String[] words) {
        if(words==null||words.length<=0) {
            return words;
        }
        String res= "";
        for (int i = 0; i <words.length ; i++) {
            if(isKeyBoardRow(words[i].toUpperCase())){
                res+=words[i]+" ";

            }
        }
        String[] s=res.split(" ");
        if(s[0].equals("")) {
            return new String[0];
        }
        return s;
    }
    public  boolean isKeyBoardRow(String str){
        String row1="QWERTYUIOP";
        String row2="ASDFGHJKL";
        String row3="ZXCVBNM";
        int r1=findLine(str,row1);
        int r2=findLine(str,row2);
        int r3=findLine(str,row3);
        if(r1==1||r2==1||r3==1) {
            return true;
        }

        return false;
    }
    public  int findLine(String strSon,String str){
        for (int i = 0; i <strSon.length() ; i++) {
            if(str.indexOf(strSon.charAt(i))<0) {
                return 0;
            }
        }
        return 1;
    }
    @Test
    public void test() {
     /*   String J = "aA", S = "aAAbbbb";
        System.out.println(numJewelsInStones(J,S));*/

     String[] strings = {"abc","b","aaa"};
        System.out.println(f(strings));

    }
}
